package com.itheima.homework.Demo7;

import java.io.*;

// 学生类
public class Student implements Serializable{

    private String name;
    private transient int age;// 省略有参和无参的构造    // 省略getter,setter方法    // 省略重写toString

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

// 测试类 public
class ObjectStream {
    public static void main(String[] args) throws Exception {

        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("day16_dcp\\stu.txt"));
        Student stu = new Student("张三", 13);
        oos.writeObject(stu);
        System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("day16_dcp\\stu.txt"));
        Student student = (Student) ois.readObject();
        System.out.println(student);
    }
}
